package com.mchat.communication.process.frame;

import com.mchat.communication.process.frame.impl.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Component
public class FrameProcessorFactory {
    private final Map<FrameType, FrameProcessor> processors = new HashMap<>();
    @Resource
    private BinaryFrameProcessor binaryFrameProcessor;
    @Resource
    private CloseFrameProcessor closeFrameProcessor;
    @Resource
    private PingFrameProcessor pingFrameProcessor;
    @Resource
    private PongFrameProcessor pongFrameProcessor;
    @Resource
    private TextFrameProcessor textFrameProcessor;
    @Resource
    private ContinuationFrameProcessor continuationFrameProcessor;


    public FrameProcessor getProcessor() {
        return getProcessor(FrameType.TextWebSocketFrame);
    }

    public FrameProcessor getProcessor(FrameType type) {
        if (FrameType.BinaryWebSocketFrame.equals(type)) {
            return processors.computeIfAbsent(type, name -> binaryFrameProcessor);
        } else if (FrameType.CloseWebSocketFrame.equals(type)) {
            return processors.computeIfAbsent(type, name -> closeFrameProcessor);
        } else if (FrameType.ContinuationWebSocketFrame.equals(type)) {
            return processors.computeIfAbsent(type, name -> continuationFrameProcessor);
        } else if (FrameType.PingWebSocketFrame.equals(type)) {
            return processors.computeIfAbsent(type, name -> pingFrameProcessor);
        } else if (FrameType.PongWebSocketFrame.equals(type)) {
            return processors.computeIfAbsent(type, name -> pongFrameProcessor);
        }
        return processors.computeIfAbsent(type, name -> textFrameProcessor);
    }
}
